/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.pages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.client.services.Service;
import com.sinai.mshab.client.services.ServiceAsync;
import com.sinai.mshab.client.ui.aggregate.FormData;
import com.sinai.mshab.client.ui.aggregate.Page;
import com.sinai.mshab.client.ui.aggregate.PageContainer;
import com.sinai.mshab.client.ui.basic.TextQuestionComponent;

public class ForgotPassword extends Page {

	public ForgotPassword(PageContainer page_container) {
		super(page_container);
	}
	
	private TextQuestionComponent username;

	public void initialize() {
		
		container.getEastPanel().setVisible(false);
		container.getWestPanel().setVisible(false);

		// adding a title
		Label title = new Label("Lost Password");
		title.setStyleName("h3");

		// The explanation of survey
		Label greeting = new Label(
				"If you would like to reset your password, fill out the form below" +
				" and we will send you an email at the address you gave when you " +
				"registered to start the process of resetting your password.");
		greeting.setStyleName("explanation");
		
		username = new TextQuestionComponent("username", "users", "My username is:");
		
		Button submit = new Button("Email New Password");
		submit.addClickListener(new ForgotListener());
		submit.setStyleName("register_button");
		
		this.add(title);
		this.add(greeting);
		this.add(username);
		this.add(submit);
	}
	
	/**
	 * This listener is attached to the button that retrieves passwords
	 */
	private class ForgotListener implements ClickListener {
		public void onClick(Widget sender) {
			String user = username.getAnswer().getFirstAnswer();
			
			ServiceAsync formsService = (ServiceAsync) GWT
			.create(Service.class);
			ServiceDefTarget target = (ServiceDefTarget) formsService;
			String relativeUrl = GWT.getModuleBaseURL() + FormData.getServletUrl();
			target.setServiceEntryPoint(relativeUrl);
			formsService.resetPassword(user, null);
			container.loadPage("Login");
		}
		
	}
}
